﻿using Adp;
using Microsoft.AspNetCore.Mvc;
using Yqd.Models;
using System.Linq;
using Yqd.Controllers.Filter;
using Adp.Common;
using System;
using Yqd.Core;
using System.Collections.Generic;

namespace Yqd.Controllers
{
    [Area("Yqd")]
    public class WxSpecialTaskTypeController : Controller
    {
        YqdContext ctx;

        public WxSpecialTaskTypeController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        [User]
        public ActionResult GetList()
        {
            var TaskID = Form.GetInt("TaskID");
            var key = Form.GetString("key");
            var page = Form.GetInt("page", 1);
            var pagesize = Form.GetInt("pagesize", 0);
            var r = ctx.Yqd_SpecialTaskType.AsQueryable();
            if (TaskID > 0)
            {
                r = r.Where(it => it.TaskID == TaskID);
            }
            if (key.Trim() != "")
            {
                r = r.Where(it => it.Name.ToLower().Trim().Contains(key.ToLower().Trim()));
            }
            var tasks = ctx.Yqd_SpecialTask.Select(it => new { it.TaskID, it.Name }).ToList();
            return this.ToJson(new
            {
                success = true,
                totalcount = r.Count(),
                data = r.OrderBy(it => it.Tactic).Skip((page - 1) * pagesize).Take(pagesize).ToList().Select(it => new
                {
                    it.TaskID,
                    it.TypeID,
                    TaskName = tasks.FirstOrDefault(b => b.TaskID == it.TaskID)?.Name,
                    TypeName = it.Name,
                    it.Remark,
                    Instime = it.Instime.ToString("yyyy-MM-dd")
                })
            });
        }

        [User]
        public ActionResult GetTypeList()
        {
            var TaskID = Form.GetInt("TaskID");
            return this.ToJson(new
            {
                success = true,
                data = ctx.Yqd_SpecialTaskType.Where(it => it.TaskID == TaskID).OrderBy(it => it.Tactic).ToList().Select(it => new
                {
                    text = it.Name,
                    value = it.TypeID
                })
            });
        }

        /// <summary>
        /// 添加专项任务类别
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Add(Yqd_SpecialTaskType model)
        {
            model.Tactic = ctx.Database.MaxID("Yqd_SpecialTaskType", "Tactic");
            model.Instime = DateTime.Now;
            model.EmployeeCode = WxUser.LogonInfo.EmployeeCode;
            model.UnitCode = WxUser.LogonInfo.UnitCode;
            ctx.Yqd_SpecialTaskType.Add(model);
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        /// <summary>
        /// 修改专项任务类别
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Edit(Yqd_SpecialTaskType model)
        {
            var tasktype = ctx.Yqd_SpecialTaskType.FirstOrDefault(it => it.TypeID == model.TypeID);
            if (tasktype != null)
            {
                tasktype.TaskID = model.TaskID;
                tasktype.Name = model.Name;
                tasktype.Remark = model.Remark;
            }
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        [User]
        public ActionResult Delete()
        {
            var TypeID = Form.GetInt("TypeID");
            var ret = ctx.Database.ExecuteSql("delete from Yqd_SpecialTaskType where TypeID={0};", TypeID);
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }
    }
}
